package zhihui.mobile.ui.register;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;

import com.zgm.utils.ui.UiHeaderLayout;

import zhihui.mobile.R;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.register.IRegisterContract;
import zhihui.mobile.presenter.register.RegisterPersenter;
import zhihui.mobile.ui.BaseActivity;
import zhihui.mobile.ui.FragmentFactory;

/**
 * Created by 张公铭 on 2017/7/3 0003.
 */

public class RegisterActivity extends BaseActivity implements IRegisterContract.IRegisterView
{
    private IRegisterContract.IRegisterPersenter registerPersenter = new RegisterPersenter();

    private UiHeaderLayout mHeader;
    private ImageView iv_one;
    private ImageView iv_two;
    private ImageView iv_thress;
    private Fragment curFragment;

    @Override
    public void initView()
    {
        mHeader = (UiHeaderLayout) findViewById(R.id.header_register);
        mHeader.setTitle("注册");
        mHeader.hindRightOneButton();
        mHeader.hindRightTwoButton();
        mHeader.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goBack(RegisterActivity.class);
            }
        });

        iv_one = (ImageView) findViewById(R.id.iv_register_one);
        iv_one.setImageResource(R.drawable.one_focuse);

        iv_two = (ImageView) findViewById(R.id.iv_register_two);
        iv_two.setImageResource(R.drawable.two_normal);

        iv_thress = (ImageView) findViewById(R.id.iv_register_three);
        iv_thress.setImageResource(R.drawable.three_normal);

        showAddressFragment(true);
    }


    @Override
    public void goBack(Class cls)
    {
        if (curFragment instanceof AddressFragment)
        {
            super.goBack(RegisterActivity.class);
        } else if (curFragment instanceof PhoneFragment)
        {
            showAddressFragment(false);
        } else if (curFragment instanceof PhoneCodeFragment)
        {
            showPhoneFragment();
        } else if (curFragment instanceof PasswordFragment)
        {
            showPhoneCodeFragment();
        } else
        {
            super.goBack(RegisterActivity.class);
        }
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.register_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{registerPersenter};
    }

    @Override
    protected void onInitPresenter()
    {
        registerPersenter.init(this, this);
    }

    @Override
    public void showAddressFragment(boolean isLoaction)
    {
        updateTitleIcon(1);
        if (curFragment != null && curFragment instanceof AddressFragment && curFragment.isVisible())
        {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (curFragment != null)
        {
            transaction.remove(curFragment);
            transaction.commit();
            transaction = getSupportFragmentManager().beginTransaction();
        }
        AddressFragment fragment = FragmentFactory.create(this, AddressFragment.class, FragmentFactory.ADDRESS_FRAGMENT);
        curFragment = fragment;
        fragment.setRegisterPersenter((RegisterPersenter) registerPersenter);
        fragment.setLoaction(isLoaction);
        transaction.replace(R.id.fm_register_content, curFragment, FragmentFactory.ADDRESS_FRAGMENT);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void showPhoneFragment()
    {
        updateTitleIcon(2);
        if (curFragment != null && curFragment instanceof PhoneFragment && curFragment.isVisible())
        {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (curFragment != null)
        {
            transaction.remove(curFragment);
            transaction.commit();
            transaction = getSupportFragmentManager().beginTransaction();
        }
        PhoneFragment phoneFragment = FragmentFactory.create(this, PhoneFragment.class, FragmentFactory.PHONE_FRAGMENT);
        curFragment = phoneFragment;
        phoneFragment.setRegisterPersenter((RegisterPersenter) registerPersenter);
        transaction.replace(R.id.fm_register_content, curFragment, FragmentFactory.PHONE_FRAGMENT);
        transaction.commitAllowingStateLoss();
    }


    @Override
    public void showPhoneCodeFragment()
    {
        updateTitleIcon(2);
        if (curFragment != null && curFragment instanceof PhoneCodeFragment && curFragment.isVisible())
        {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (curFragment != null)
        {
            transaction.remove(curFragment);
            transaction.commit();
            transaction = getSupportFragmentManager().beginTransaction();
        }
        PhoneCodeFragment phoneCodeFragment = FragmentFactory.create(this, PhoneCodeFragment.class, FragmentFactory.PHONE_CODE_FRAGMENT);
        curFragment = phoneCodeFragment;
        phoneCodeFragment.setRegisterPersenter(registerPersenter);
        transaction.replace(R.id.fm_register_content, curFragment, FragmentFactory.PHONE_CODE_FRAGMENT);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void showPasswordFragment()
    {
        updateTitleIcon(3);
        if (curFragment != null && curFragment instanceof PasswordFragment && curFragment.isVisible())
        {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (curFragment != null)
        {
            transaction.remove(curFragment);
            transaction.commit();
            transaction = getSupportFragmentManager().beginTransaction();
        }
        PasswordFragment passwordFragment = FragmentFactory.create(this, PasswordFragment.class, FragmentFactory.PASSWORD_FRAGMENT);
        curFragment = passwordFragment;
        passwordFragment.setRegisterPersenter((RegisterPersenter) registerPersenter);
        transaction.replace(R.id.fm_register_content, curFragment, FragmentFactory.PASSWORD_FRAGMENT);
        transaction.commitAllowingStateLoss();
    }

    private void updateTitleIcon(int type)
    {
        iv_one.setImageResource(R.drawable.one_normal);
        iv_two.setImageResource(R.drawable.two_normal);
        iv_thress.setImageResource(R.drawable.three_normal);
        switch (type)
        {
            case 1:
                iv_one.setImageResource(R.drawable.one_focuse);
                break;
            case 2:
                iv_two.setImageResource(R.drawable.two_focuse);
                break;
            case 3:
                iv_thress.setImageResource(R.drawable.three_focuse);
                break;
        }

    }
}
